package com.eleven.mall.controller;

import com.eleven.mall.pojo.ManagerPage;
import com.eleven.mall.pojo.Rbody;
import com.eleven.mall.pojo.SystemUser;
import com.eleven.mall.pojo.UserIn;
import com.eleven.mall.pojo.entity.Manager;
import com.eleven.mall.service.ManagerService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: Rui_simple
 * @Date: 2022/10/24 11:15
 * @Description: ManagerController
 * @Version 1.0.0
 */
@RestController
@AllArgsConstructor
@CrossOrigin
public class ManagerController {

    ManagerService managerService;

    @GetMapping("/users")
    public Rbody getUserList(String query, Integer pagenum, Integer pagesize){
        System.out.println("==========查询参数:"+query+" pagenum:"+pagenum+" pagesize:"+pagesize);
        Rbody rbody = new Rbody();
        ManagerPage managerPage = managerService.getUserList(query, pagenum, pagesize);
        rbody.setData(managerPage);
        System.out.println(managerPage);
        rbody.getMeta().setMsg("获取成功");
        return rbody;
    }

    @PostMapping("/users")
    public Rbody addUser(@RequestBody Manager manager){
        //System.out.println(manager);
        Manager systemUser = managerService.addUser(manager);
        Rbody rbody = new Rbody();
        if (systemUser == null){
            rbody.getMeta().setStatus(500);
            rbody.getMeta().setMsg("创建用户失败");
        }else{
            rbody.getMeta().setStatus(201);
            rbody.getMeta().setMsg("创建用户成功");
            rbody.setData(systemUser);
            System.out.println(systemUser);
        }
        return rbody;
    }

    @PutMapping("/users/{id}/state/{type}")
    public Rbody changeUserType(@PathVariable("id") Integer id,@PathVariable("type") Boolean type){
        //System.out.println("用户Id："+id+" 用户状态："+type);
        Manager manager = managerService.changeUserType(id, type);
        Rbody rbody = new Rbody();
        rbody.setData(manager);
        return rbody;
    }

    @GetMapping("/users/{id}")
    public Rbody getUserById(@PathVariable("id") Integer id){
        Rbody rbody = new Rbody();
        Manager manager = managerService.getUserById(id);

        if (manager == null){
            rbody.getMeta().setStatus(500);
            rbody.getMeta().setMsg("获取失败");
        }else{
            rbody.setData(manager);
        }
        return rbody;
    }

    @PutMapping("/users/{id}")
    public Rbody changeUser(@PathVariable("id") Integer id,@RequestBody Manager manager){
        //System.out.println("id是："+id+" 其他用户信息是："+manager);
        Manager managerout = managerService.changeUser(id, manager);
        Rbody rbody = new Rbody();
        rbody.setData(managerout);
        rbody.getMeta().setMsg("更新成功");
        return rbody;
    }

    @DeleteMapping("/users/{id}")
    public Rbody deleteUser(@PathVariable("id") Integer id){
        Rbody rbody = new Rbody();
        Boolean flag = managerService.deleteUser(id);
        if (!flag){
            rbody.getMeta().setStatus(500);
            rbody.getMeta().setMsg("删除失败");
        }
        rbody.getMeta().setMsg("删除成功");
        return rbody;
    }

    @PutMapping("/users/{id}/role")
    public Rbody changeUserRole(@PathVariable("id") Integer id,@RequestBody Manager manager){
        //System.out.println("id是："+id+" rid："+manager.getRoleId());
        //System.out.println(manager);
        Manager managerOut = managerService.changeUserRole(id,manager);
        Rbody rbody = new Rbody();
        rbody.getMeta().setMsg("设置角色成功");
        rbody.setData(managerOut);
        return rbody;
    }

}
